package cn.blunce.weather.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.blunce.weather.base.AppDB;
import cn.blunce.weather.models.AstroWeather;
import cn.blunce.weather.models.CityID;
import cn.blunce.weather.models.DailyForcast;
import cn.blunce.weather.models.HourlyForcast;
import cn.blunce.weather.models.Suggestion;
import cn.blunce.weather.models.WeatherAQI;
import cn.blunce.weather.models.WeatherDaily;
import cn.blunce.weather.models.WeatherID;
import cn.blunce.weather.models.WeatherMessage;
import cn.blunce.weather.models.WeatherNow;
import cn.blunce.weather.models.WeatherNowCond;
import cn.blunce.weather.models.WeatherSuggestion;
import cn.blunce.weather.models.WeatherTmp;
import cn.blunce.weather.models.WeatherWind;

public class ParseUtils {
    public static List<WeatherID> parseWeathers(String weathers) {
        try {
            JSONObject jsonObject = new JSONObject(weathers);
            return new Gson().fromJson(jsonObject
                    .getString("cond_info"), new TypeToken<List<WeatherID>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public static List<CityID> parseCities(String cities) throws Exception {
        JSONObject jsonObject = new JSONObject(cities);
        return new Gson().fromJson(jsonObject
                .getString("city_info"), new TypeToken<List<CityID>>() {
        }.getType());
    }


    public static WeatherMessage ParseAndAddWeatherMessage(String string, AppDB appDB) throws Exception {
        JSONObject jsonObject = new JSONObject(string);
        JSONObject message = new JSONArray(jsonObject.getString("HeWeather data service 3.0"))
                .getJSONObject(0);

        WeatherMessage weatherMessage = new WeatherMessage();
        weatherMessage.setId(getLongID());
        weatherMessage.setCity_id(message.getJSONObject("basic").getString("id"));
        weatherMessage.setUpdate(message.getJSONObject("basic").getJSONObject("update")
                .getString("loc"));

        WeatherAQI weatherAPI = new Gson().fromJson(message.getJSONObject("aqi")
                .getString("city"), WeatherAQI.class);
        weatherAPI.setId(getLongID());
        appDB.add(weatherAPI);
        weatherMessage.setAqi(weatherAPI);

        JSONArray jsons_dailyforcast = message.getJSONArray("daily_forecast");
        for (int i = 0; i < jsons_dailyforcast.length(); i++) {
            DailyForcast dailyForcast = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create().fromJson(jsons_dailyforcast.getString(i), DailyForcast.class);
            dailyForcast.setId(getLongID());

            AstroWeather astroWeather = new Gson().fromJson(jsons_dailyforcast.getJSONObject(i)
                    .getString("astro"), AstroWeather.class);
            astroWeather.setId(getLongID());
            appDB.add(astroWeather);
            dailyForcast.setAstro(astroWeather);

            WeatherDaily weatherDaily = new Gson().fromJson(jsons_dailyforcast.getJSONObject(i)
                    .getString("cond"), WeatherDaily.class);
            weatherDaily.setId(getLongID());
            appDB.add(weatherDaily);
            dailyForcast.setCond(weatherDaily);

            WeatherTmp weatherTmp = new Gson().fromJson(jsons_dailyforcast.getJSONObject(i)
                    .getString("tmp"), WeatherTmp.class);
            weatherTmp.setId(getLongID());
            appDB.add(weatherTmp);
            dailyForcast.setTmp(weatherTmp);

            WeatherWind weatherWind = new Gson().fromJson(jsons_dailyforcast
                    .getJSONObject(i).getString("wind"), WeatherWind.class);
            weatherWind.setId(getLongID());
            appDB.add(weatherWind);
            dailyForcast.setWind(weatherWind);

            dailyForcast.setWeather_message_id(weatherMessage.getId());
            appDB.add(dailyForcast);
        }

        JSONArray jsons_hourlyforcast = message.getJSONArray("hourly_forecast");
        for (int i = 0; i < jsons_hourlyforcast.length(); i++) {
            HourlyForcast hourlyForcast = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create().fromJson(jsons_hourlyforcast.getString(i), HourlyForcast.class);
            hourlyForcast.setId(getLongID());

            WeatherWind weatherWind = new Gson().fromJson(jsons_hourlyforcast.getJSONObject(i)
                    .getString("wind"), WeatherWind.class);
            weatherWind.setId(getLongID());
            appDB.add(weatherWind);
            hourlyForcast.setWind(weatherWind);

            hourlyForcast.setWeather_message_id(weatherMessage.getId());
            appDB.add(hourlyForcast);
        }

        //debugging
        WeatherNow weatherNow = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                .create().fromJson(message.getString("now"), WeatherNow.class);
        weatherNow.setId(getLongID());

        WeatherWind weatherWind = new Gson().fromJson(message.getJSONObject("now").getString("wind"), WeatherWind.class);
        weatherWind.setId(getLongID());
        appDB.add(weatherWind);
        weatherNow.setWind(weatherWind);

        WeatherNowCond weatherNowCond = new Gson().fromJson(message.getJSONObject("now").getString("cond"), WeatherNowCond.class);
        weatherNowCond.setId(getLongID());
        appDB.add(weatherNowCond);
        weatherNow.setCond_now(weatherNowCond);

        weatherMessage.setNow(weatherNow);


        WeatherSuggestion weatherSuggsetion = new WeatherSuggestion(getLongID());

        Suggestion suggestion_comf = new Gson().fromJson(message.getJSONObject("suggestion").getString("comf"), Suggestion.class);
        suggestion_comf.setId(getLongID());
        appDB.add(suggestion_comf);
        weatherSuggsetion.setComf(suggestion_comf);

        Suggestion suggestion_cw = new Gson().fromJson(message.getJSONObject("suggestion").getString("cw"), Suggestion.class);
        suggestion_cw.setId(getLongID());
        appDB.add(getLongID());
        weatherSuggsetion.setCw(suggestion_cw);

        Suggestion suggestion_drsg = new Gson().fromJson(message.getJSONObject("suggestion").getString("drsg"), Suggestion.class);
        suggestion_drsg.setId(getLongID());
        appDB.add(suggestion_drsg);
        weatherSuggsetion.setDrsg(suggestion_drsg);

        Suggestion suggestion_flu = new Gson().fromJson(message.getJSONObject("suggestion").getString("flu"), Suggestion.class);
        suggestion_flu.setId(getLongID());
        appDB.add(suggestion_flu);
        weatherSuggsetion.setFlu(suggestion_flu);

        Suggestion suggestion_sport = new Gson().fromJson(message.getJSONObject("suggestion").getString("sport"), Suggestion.class);
        suggestion_sport.setId(getLongID());
        appDB.add(suggestion_sport);
        weatherSuggsetion.setSport(suggestion_sport);

        Suggestion suggestion_trav = new Gson().fromJson(message.getJSONObject("suggestion").getString("trav"), Suggestion.class);
        suggestion_trav.setId(getLongID());
        appDB.add(suggestion_trav);
        weatherSuggsetion.setTraw(suggestion_trav);

        Suggestion suggestion_uv = new Gson().fromJson(message.getJSONObject("suggestion").getString("uv"), Suggestion.class);
        suggestion_trav.setId(getLongID());
        appDB.add(suggestion_uv);
        weatherSuggsetion.setUv(suggestion_uv);

        appDB.add(weatherSuggsetion);

        weatherMessage.setSuggestion(weatherSuggsetion);

        appDB.add(weatherMessage);

        return weatherMessage;
    }

    private static Long getLongID() {
        return System.currentTimeMillis();
    }
}
